package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.*;
import com.itgaoshu.hospitalproject.service.DepartmentsService;
import com.itgaoshu.hospitalproject.service.ProjecttypeService;
import com.itgaoshu.hospitalproject.service.UnitService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("prjectType")
public class ProjecttypeController {
    @Resource
    private ProjecttypeService projecttypeService;

    //查询项目大类的方法
    @RequestMapping("findAllProjecttype")
    @ResponseBody
    public Object findAllProjecttype(String projectName, Integer page, Integer limit){
        PageHelper.startPage(page,limit);
        List<Projecttype> projecttypes = projecttypeService.findAllProjecttype(projectName);
        PageInfo pageInfo = new PageInfo(projecttypes);
        Map<String,Object> tableData = new HashMap<>();
        tableData.put("msg","");
        tableData.put("code",0);
        tableData.put("data",pageInfo.getList());
        tableData.put("count",pageInfo.getTotal());
        return tableData;
    }


    //添加项目大类的方法
    @ResponseBody
    @RequestMapping("addProjecttype")
    public Object addProjecttype(Projecttype projecttype){
        int count = projecttypeService.count(projecttype);
        if (count==0){
            int result = projecttypeService.addProjecttype(projecttype);
            if (result>0){
                return "添加成功";
            }else{
                return "修改失败";
            }
        }else{
            return "这个项目大类已经存在";
        }
    }

//===========================================
    //查询所有病床的方法
    @RequestMapping("findAllBed")
    @ResponseBody
    public Object findAllBed(String bedname ,Integer page, Integer limit){
        PageHelper.startPage(page,limit);
        List<Bed> beds= projecttypeService.findAllBed(bedname);
        PageInfo pageInfo = new PageInfo(beds);
        Map<String,Object> tableData = new HashMap<>();
        tableData.put("msg","");
        tableData.put("code",0);
        tableData.put("data",pageInfo.getList());
        tableData.put("count",pageInfo.getTotal());
        return tableData;
    }


    //添加病床的方法
    @RequestMapping("addBed")
    @ResponseBody
    public Object addBed(Bed bed,Integer departmentId){
        bed.setDepartmentId(departmentId);
        int count1 = projecttypeService.count1(bed);
        if(count1==0){
            int result = projecttypeService.addBed(bed);
            if(result>0){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else {
            return "这个病床已存在";
        }

    }

    //查询所有科室
    @Autowired
    private DepartmentsService departmentsService;
    @ResponseBody
    @RequestMapping("departmentList")
    public Object departmentList(){
        return departmentsService.departmentList(null);
    }


    //修改床位
    @RequestMapping("editBed")
    @ResponseBody
    public Object editBed(Bed bed,Integer departmentId){
        bed.setDepartmentId(departmentId);
        int result = projecttypeService.editBed(bed);
        if(result>0){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }


    /*
     * 删除床位
     * */
    @RequestMapping("deleteBed")
    @ResponseBody
    public Object deleteBed(Integer bedId){
        int count = projecttypeService.count2(bedId);
        if(count==0){
            int i = projecttypeService.deleteBed(bedId);
            if(i==1){
                return "删除成功";
            }else{
                return "删除失败";
            }

        }else {
            return "这还有病人哦" ;
        }

    }




//===============================================================
    //查询收费类型
    @RequestMapping("findAllMoneytype")
    @ResponseBody
    public Object findAllMoneytype(String moneytype, Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        List<Moneytype> listAll = projecttypeService.findAllMoneytype(moneytype);
        PageInfo pageInfo = new PageInfo(listAll);
        Map<String, Object> tableData = new HashMap<String, Object>();
        tableData.put("code", 0);
        tableData.put("msg", "");
        tableData.put("count", pageInfo.getTotal());
        tableData.put("data", pageInfo.getList());
         return tableData;
    }

    //添加收费
    @RequestMapping("addMoneytype")
    @ResponseBody
    public Object addMoneytype(Moneytype moneytype){
        int count = projecttypeService.count3(moneytype);
        if(count==0){
            int result = projecttypeService.addMoneytype(moneytype);
            if(result>0){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else{
            return "这个收费类型已经存在";
        }

    }


    //修改收费类型
    @RequestMapping("editMoneytype")
    @ResponseBody
    public Object editMoneytype(Moneytype moneytype){
        int result = projecttypeService.editMoneytype(moneytype);
        if(result>0){
            return "修改成功";
        }else{
            return "修改失败";
        }

    }


    //删除类型
    @RequestMapping("deleteMoneytype")
    @ResponseBody
    public Object deleteMoneytype(Integer moneyId){
        int result = projecttypeService.deleteMoneytype(moneyId);
        if(result>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }






//================================================================
    //门诊收费
    @RequestMapping("findAllOutpatienttype")
    @ResponseBody
    public Object findAllOutpatienttype(String projectName, Integer page, Integer limit){

        PageHelper.startPage(page, limit);
        List<Outpatienttype> listAll = projecttypeService.findAllOutpatienttype(projectName);
        PageInfo pageInfo = new PageInfo(listAll);
        Map<String, Object> tableData = new HashMap<String, Object>();
        tableData.put("code", 0);
        tableData.put("msg", "");
        tableData.put("count", pageInfo.getTotal());
        tableData.put("data", pageInfo.getList());

        return tableData;
    }

    //添加
    @RequestMapping("addOutpatienttype")
    @ResponseBody
    public Object addOutpatienttype(Outpatienttype outpatienttype,Integer projectId,Integer unitId){
        outpatienttype.setBigprojectId(projectId);
        outpatienttype.setUnit(unitId);
        int count = projecttypeService.count4(outpatienttype);
        if(count==0){
            int result = projecttypeService.addOutpatienttype(outpatienttype);
            if(result>0){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else {
            return "这个门诊收费类型已存在";
        }
     }

     //查询所有产地
    @Autowired
    private UnitService unitService;
    @RequestMapping("findAllUnit")
    @ResponseBody
    public Object UnitList(){
        List<Unit> listAll = unitService.findAllUnit(null);
        return listAll;
    }
    //查询所有产品类型
    @RequestMapping("findAllProjecttype1")
    @ResponseBody
    public Object ProjecttypeList(){
         List<Projecttype> listAll = projecttypeService.findAllProjecttype(null);
         return listAll;
    }

    //修改
    @RequestMapping("editOutpatienttype")
    @ResponseBody
    public Object editOutpatienttype(Outpatienttype outpatienttype,Integer projectId,Integer unitId) {
        outpatienttype.setBigprojectId(projectId);
        outpatienttype.setUnit(unitId);
        int result = projecttypeService.editOutpatienttype(outpatienttype);
        if (result>0) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    //删除
    @RequestMapping("deleteOutpatienttype")
    @ResponseBody
    public Object deleteOutpatienttype(Integer outpatientId){
        int result = projecttypeService.deleteOutpatienttype(outpatientId);
        if(result>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }




//===========================================================
    //查询所有住院收费项目
    @RequestMapping("findAllInoutpatienttype")
    @ResponseBody
    public Object InoutpatienttypeList(String projectName, Integer page, Integer limit){

        PageHelper.startPage(page, limit);
        List<Inoutpatienttype> listAll = projecttypeService.findAllInoutpatienttype(projectName);
        PageInfo pageInfo = new PageInfo(listAll);
        Map<String, Object> tableData = new HashMap<String, Object>();
        tableData.put("code", 0);
        tableData.put("msg", "");
        tableData.put("count", pageInfo.getTotal());
        tableData.put("data", pageInfo.getList());
         return tableData;
    }

    //添加住院收费
    @RequestMapping("addInoutpatienttype")
    @ResponseBody
    public Object addInoutpatienttype(Inoutpatienttype inoutpatienttype,Integer projectId,Integer unitId){
        inoutpatienttype.setBigprojectId(projectId);
        inoutpatienttype.setUnit(unitId);
        int  count= projecttypeService.count5(inoutpatienttype);
        if(count==0){
            int result = projecttypeService.addInoutpatienttype(inoutpatienttype);
            if(result>0){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else {
            return inoutpatienttype.getProjectName()+"已存在";
        }
     }

    //查询计量
    @RequestMapping("findAllUnit1")
    @ResponseBody
    public Object findAllUnit1(){
        return unitService.findAllUnit(null);
    }
    //项目大类
    @RequestMapping("findAllProjecttype2")
    @ResponseBody
    public Object findAllProjecttype2(){
        return projecttypeService.findAllProjecttype(null);
    }

    //修改住院
    @RequestMapping("editInoutpatienttype")
    @ResponseBody
    public Object editInoutpatienttype(Inoutpatienttype inoutpatienttype,Integer projectId,Integer unitId){
        inoutpatienttype.setBigprojectId(projectId);
        inoutpatienttype.setUnit(unitId);
        int result = projecttypeService.editInoutpatienttype(inoutpatienttype);
        if(result>0){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }


    //删除住院
    @RequestMapping("deleteInoutpatienttype")
    @ResponseBody
    public Object deleteInoutpatienttype(Integer inoutpatientId){
        int result = projecttypeService.deleteInoutpatienttype(inoutpatientId);
        if(result>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }


}
